package mspbots.liongard.client.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import mspbots.liongard.client.core.client.LiongardClient;
import mspbots.liongard.common.RabbitKeys;
import mspbots.liongard.common.ResultMsg;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author billb
 */
@Log4j2
public abstract class AbstractToolsUtil {

    private ReactiveStringRedisTemplate stringRedisTemplate;
    protected ReactiveRedisTemplate<String, Object> reactiveRedisTemplate;
    protected ObjectMapper objectMapper;
    protected WebClient dataClient;
    protected LiongardClient liongardClient;

    private RabbitMessagingTemplate rabbitMessagingTemplate;

    protected void sendEntity(String queueKey,ResultMsg resultMsg) {
        this.rabbitMessagingTemplate.convertAndSend(RabbitKeys.BASE_EXCHANGE_NAME,
                queueKey, resultMsg);
    }

    protected Mono<LocalDateTime> readAnchorTime(String key) {
        return this.stringRedisTemplate.opsForValue().get(key)
                .map(LocalDateTime::parse)
                .defaultIfEmpty(LocalDateTime.now().minusYears(20));
    }

    protected void recordAnchorTime(String key, String value) {
        LocalDateTime nowAnchorTime = LocalDateTime.parse(value, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        this.readAnchorTime(key)
                .filter(v -> v.isBefore(nowAnchorTime))
                .flatMap(v -> this.stringRedisTemplate.opsForValue()
                        .set(key, nowAnchorTime.format(DateTimeFormatter.ISO_DATE_TIME), Duration.ofDays(30)))
                .subscribe();
    }

    @Autowired
    public void setSmileBackClient(LiongardClient liongardClient) {
        this.liongardClient = liongardClient;
    }

    @Autowired
    public void setDataClient(WebClient.Builder builder,
                              ReactorLoadBalancerExchangeFilterFunction filterFunction) {
        this.dataClient = builder.baseUrl("http://data-teams/")
                .filter(filterFunction)
                .build();
    }

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setMessagingTemplate(RabbitMessagingTemplate messagingTemplate) {
        this.rabbitMessagingTemplate = messagingTemplate;
    }

    @Autowired
    public void setStringRedisTemplate(ReactiveStringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Autowired
    public void setReactiveRedisTemplate(ReactiveRedisTemplate<String, Object> redisTemplate) {
        this.reactiveRedisTemplate = redisTemplate;
    }


}
